package se.sudoku;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;


public class SudokuMenu extends Activity {

	//SUDOKUMENU ITEMS
	public String IP;
	private PopupWindow sudokumenu;
	private Button sudokumenu_btn_1, sudokumenu_btn_2, sudokumenu_btn_3, sudokumenu_btn_4, sudokumenu_btn_5, sudokumenu_btn_btmleft, sudokumenu_btn_btmright;
	
	private EditText sudokumenu_textfield;
	private ImageView sudokumenu_titleImage;
	private LinearLayout sudokumenu_btmbtnholder;
	private int menuIndex;
	private Context context;
	private Activity activity;
	private Spinner s;
	private ArrayAdapter<CharSequence> adapter; 
	
	public final int DIFF_EASY = 1;
	public final int DIFF_MEDIUM = 2;
	public final int DIFF_HARD = 4;
	 
	//constructor
	public SudokuMenu(Context c) {
		activity = (Activity) c;
		context = c;
		//popup window
		sudokumenu = new PopupWindow(View.inflate(c, R.layout.sudokumenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);	    
		sudokumenu.setAnimationStyle(R.style.anim_fade);		
		//title image
		sudokumenu_titleImage = (ImageView)sudokumenu.getContentView().findViewById(R.id.sudokumenu_titleimage);
		//btm btn holder
		sudokumenu_btmbtnholder = (LinearLayout)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btmbtnsholder);
		//textfield
		sudokumenu_textfield = (EditText)sudokumenu.getContentView().findViewById(R.id.sudokumenu_textfield);
		
		
		//Spinner
		s = (Spinner)sudokumenu.getContentView().findViewById(R.id.sudokumenu_spinner);
		adapter = ArrayAdapter.createFromResource(
	            context, R.array.difficulties, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    s.setAdapter(adapter);
		
		//buttons here
		sudokumenu_btn_1 = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn1);
		sudokumenu_btn_1.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_2 = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn2);
	    sudokumenu_btn_2.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_3 = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn3);
	    sudokumenu_btn_3.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_4 = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn4);
	    sudokumenu_btn_4.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_5 = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn5);
	    sudokumenu_btn_5.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_btmleft = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn_btmleft);
	    sudokumenu_btn_btmleft.setOnClickListener(sudokuMenuListener);
	    sudokumenu_btn_btmright = (Button)sudokumenu.getContentView().findViewById(R.id.sudokumenu_btn_btmright);
	    sudokumenu_btn_btmright.setOnClickListener(sudokuMenuListener);	
	}
	
	//function that empties the popup
	public void emptySudokuMenu(){
		sudokumenu_titleImage.setVisibility(View.GONE);
		sudokumenu_btn_1.setVisibility(View.GONE);
		sudokumenu_btn_2.setVisibility(View.GONE);
		sudokumenu_btn_3.setVisibility(View.GONE);
		sudokumenu_btn_4.setVisibility(View.GONE);
		sudokumenu_btn_5.setVisibility(View.GONE);
		sudokumenu_textfield.setVisibility(View.GONE);
		sudokumenu_btn_btmleft.setVisibility(View.GONE);
		sudokumenu_btn_btmright.setVisibility(View.GONE);
		sudokumenu_btmbtnholder.setVisibility(View.GONE);
		s.setVisibility(View.GONE);
	}
	
	// SET MAIN MENU
	public void setToMainMenu(){
		emptySudokuMenu();
		sudokumenu_titleImage.setImageResource(R.drawable.logo);
		sudokumenu_titleImage.setVisibility(View.VISIBLE);
		//buttons here
		sudokumenu_btn_1.setText("Solo");
		sudokumenu_btn_1.setVisibility(View.VISIBLE);
		sudokumenu_btn_2.setText("Multi");
		sudokumenu_btn_2.setVisibility(View.VISIBLE);
		sudokumenu_btn_3.setText("Quit");
		sudokumenu_btn_3.setVisibility(View.VISIBLE);
		menuIndex=1;
	}
	// SET SOLO SUBMENU
	public void setToSoloMenu(){
		emptySudokuMenu();
		sudokumenu_titleImage.setImageResource(R.drawable.solotitle);
		sudokumenu_titleImage.setVisibility(View.VISIBLE);
		s.setVisibility(View.VISIBLE);
		//buttons here
		sudokumenu_btmbtnholder.setVisibility(View.VISIBLE);
		sudokumenu_btn_btmleft.setText("Back");
		sudokumenu_btn_btmleft.setVisibility(View.VISIBLE);
		sudokumenu_btn_btmright.setText("Start");
		sudokumenu_btn_btmright.setVisibility(View.VISIBLE);
		menuIndex=2;
	}
	// SET MULTI SUBMENU
	public void setToMultiMenu(){
		emptySudokuMenu();
		sudokumenu_titleImage.setImageResource(R.drawable.multititle);
		sudokumenu_titleImage.setVisibility(View.VISIBLE);
		//buttons here
		sudokumenu_btn_1.setText("Create");
		sudokumenu_btn_1.setVisibility(View.VISIBLE);
		sudokumenu_btn_2.setText("Join");
		sudokumenu_btn_2.setVisibility(View.VISIBLE);
		sudokumenu_btmbtnholder.setVisibility(View.VISIBLE);
		sudokumenu_btn_btmleft.setText("Back");
		sudokumenu_btn_btmleft.setVisibility(View.VISIBLE);
		menuIndex=3;
	}
	// SET MULTI->CREATE SUBMENU
	public void setToCreateMenu(){
		emptySudokuMenu();
		sudokumenu_titleImage.setImageResource(R.drawable.createtitle);
		sudokumenu_titleImage.setVisibility(View.VISIBLE);
		s.setVisibility(View.VISIBLE);
		//buttons here
		sudokumenu_btn_1.setText("Versus");
		sudokumenu_btn_1.setVisibility(View.VISIBLE);
		sudokumenu_btn_2.setText("Battle");
		sudokumenu_btn_2.setVisibility(View.VISIBLE);
		sudokumenu_btn_3.setText("Co-op");
		sudokumenu_btn_3.setVisibility(View.VISIBLE);
		sudokumenu_btmbtnholder.setVisibility(View.VISIBLE);
		sudokumenu_btn_btmleft.setText("Back");
		sudokumenu_btn_btmleft.setVisibility(View.VISIBLE);
		menuIndex=4;
	}
	// SET MULTI->JOIN SUBMENU
	public void setToJoinMenu(){
		emptySudokuMenu();
		sudokumenu_titleImage.setImageResource(R.drawable.jointitle);
		sudokumenu_titleImage.setVisibility(View.VISIBLE);
		//buttons here
		sudokumenu_textfield.setHint("Enter Host IP");
		//sudokumenu_textfield.setText("196.172.1.1");
		sudokumenu_textfield.setVisibility(View.VISIBLE);
		sudokumenu_btn_1.setText("Connect to Versus");
		sudokumenu_btn_1.setVisibility(View.VISIBLE);
		sudokumenu_btn_2.setText("Connect to Battle");
		sudokumenu_btn_2.setVisibility(View.VISIBLE);
		sudokumenu_btn_3.setText("Connect to Co-op");
		sudokumenu_btn_3.setVisibility(View.VISIBLE);
		sudokumenu_btmbtnholder.setVisibility(View.VISIBLE);
		sudokumenu_btn_btmleft.setText("Back");
		sudokumenu_btn_btmleft.setVisibility(View.VISIBLE);
		menuIndex=5;
	}
	
	//toggle windows
	public void toggleWindow(){  	
    	if(!sudokumenu.isShowing()){
    		sudokumenu.showAtLocation(activity.findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
    	}
    	else{
    		sudokumenu.dismiss();
    	}
    }
	
	public int getChosenDifficulty(){
		
		int chosenDifficulty = s.getSelectedItemPosition();
		int temp = DIFF_EASY;
		if(chosenDifficulty==0){temp = DIFF_EASY;}
		else if(chosenDifficulty==1){temp = DIFF_MEDIUM;}
		else if(chosenDifficulty==2){temp = DIFF_HARD;}
		else {temp = DIFF_EASY;}
		
		chosenDifficulty = temp;
		return chosenDifficulty;
	}
	
	

	//start game function
	public void startGame(final int gameType, final int diff){
		activity.setContentView(R.layout.loadingscreen);
		new Thread() { 
            public void run() { 
                 try{ 
                	 SudokuAlpha.gen = new generator(diff);
		             Intent intent = new Intent(context, SudokuGame.class);
		             intent.putExtra("puzzle", SudokuAlpha.gen.getPuzzle());
		             intent.putExtra("facit", SudokuAlpha.gen.getSolution());
		             intent.putExtra("gameType", gameType);
		     		 activity.startActivityForResult(intent, 0);	    
                 } catch (Exception e) { 
                	 
                 } finally{}
                 
            } 
       }.start(); 
	}
	
	//SUDOKU MENU CLICK LISTENERS
	private OnClickListener sudokuMenuListener = new OnClickListener() {
		public void onClick(View v) {
			
	    	switch(menuIndex){
	    		//mainmenu
	    		case 1:
	    			//to solo
	    			if(v==sudokumenu_btn_1){
	    				toggleWindow();
		    			setToSoloMenu();
		    			toggleWindow();
	    			}
	    			//to multi
	    			else if(v==sudokumenu_btn_2){
	    				toggleWindow();
		    			setToMultiMenu();
		    			toggleWindow();
	    			}
	    			//quit
	    			else if(v==sudokumenu_btn_3){
	    				toggleWindow();
	    				activity.finish();
		    		}
	    			
	    		//singleplayermenu
	    		case 2:
	    			//start singleplayer game
	    			if(v==sudokumenu_btn_btmright){
		    			toggleWindow();
		    			//start game  			
		    			Log.v("difficulty",""+getChosenDifficulty());
		    			startGame(SudokuGame.SOLO_TYPE, getChosenDifficulty());  
	    			}
	    			//to main
		    		else if(v==sudokumenu_btn_btmleft){
			    		toggleWindow();
			    		setToMainMenu();
			    		toggleWindow();
			    	}    			
	    			break;
	    			
	    		//multiplayermenu
	    		case 3:
	    			//to create
	    			if(v==sudokumenu_btn_1){
		    			toggleWindow();
		    			setToCreateMenu();
		    			toggleWindow();
		    		}
	    			//to join
	    			else if(v==sudokumenu_btn_2){
		    			toggleWindow();
		    			setToJoinMenu();
		    			toggleWindow();
		    		}
	    			//to main
	    			else if(v==sudokumenu_btn_btmleft){
		    			toggleWindow();
		    			setToMainMenu();
		    			toggleWindow();
		    		}
	    			break;
	    		//createmenu
	    		case 4:
	    			//to normal
	    			if(v==sudokumenu_btn_1){
	    		        toggleWindow();
		    			//start game
	    		        startGame(SudokuGame.VERSUS_HOST_TYPE, getChosenDifficulty());
		    		}
	    			//to battle
	    			else if(v==sudokumenu_btn_2){
	    		        toggleWindow();
		    			//start game
	    		        startGame(SudokuGame.BATTLE_HOST_TYPE, getChosenDifficulty());
		    		}
	    			//to coop
	    			else if(v==sudokumenu_btn_3){
	    		        toggleWindow();
		    			//start game
	    		        startGame(SudokuGame.COOP_HOST_TYPE, getChosenDifficulty());
		    		}
	    			//back
	    			else if(v==sudokumenu_btn_btmleft){
	    				toggleWindow();
		    			setToMultiMenu();
		    			toggleWindow();
		    		}
	    			break;
	    			
	    		//joinmenu
	    		case 5:
	    			//connect to vs
	    			if(v==sudokumenu_btn_1){
	    				toggleWindow();
		    			setToJoinMenu();
		    			
		    			//get IP
		    			String IP = sudokumenu_textfield.getText().toString();
		    			
		    			Intent intent = new Intent(context, SudokuGame.class);
		    			intent.putExtra("inputIP", IP);
		    			intent.putExtra("gameType", SudokuGame.VERSUS_CLIENT_TYPE);
		    			activity.startActivityForResult(intent,0); 
		    		}
	    			//connect to battle
	    			else if(v==sudokumenu_btn_2){
	    				toggleWindow();
		    			setToJoinMenu();
		    			
		    			//get IP
		    			String IP = sudokumenu_textfield.getText().toString();
		    			
		    			Intent intent = new Intent(context, SudokuGame.class);
		    			intent.putExtra("inputIP", IP);
		    			intent.putExtra("gameType", SudokuGame.BATTLE_CLIENT_TYPE);
		    			activity.startActivityForResult(intent,0); 
		    		}
	    			//connect to co-op
	    			else if(v==sudokumenu_btn_3){
	    				toggleWindow();
		    			setToJoinMenu();
		    			//get IP
		    			String IP = sudokumenu_textfield.getText().toString();
		    			
		    			Intent intent = new Intent(context, SudokuGame.class);
		    			intent.putExtra("inputIP", IP);
		    			intent.putExtra("gameType", SudokuGame.COOP_CLIENT_TYPE);
		    			activity.startActivityForResult(intent,0);
		    		}	
	    			//back
	    			else if(v==sudokumenu_btn_btmleft){
	    				toggleWindow();
		    			setToMultiMenu();
		    			toggleWindow();
		    		}
	    			break;
	    	}  	
	    }
	};
	
}